home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TODOLIST.ARJ / TODODLGS.CPP < prev    next >
Text File  |  1991-11-11  |  15KB  |  536 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODODLGS.CPP - part of TODO example program
  4. //
  5. //      Copyright (c) 1991 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //---------------------------------------------------------------------
  9.  
  10. #if !defined( __WINDOWS_H )
  11. #include <Windows.h>
  12. #endif  // __WINDOWS_H
  13.  
  14. #if !defined( __STRING_H )
  15. #include <String.h>
  16. #endif  // __STRING_H
  17.  
  18. #if !defined( __TODODLGS_H )
  19. #include "TodoDlgs.h"
  20. #endif  // __TODODLGS_H
  21.  
  22. #if !defined( __TODODEFS_H )
  23. #include "TodoDefs.h"
  24. #endif  // __TODODEFS_H
  25.  
  26. //---------------------------------------------------------------------
  27. //
  28. //  member functions for class AboutBox.
  29. //
  30. //  not much needed here...
  31. //
  32. //---------------------------------------------------------------------
  33.  
  34. LPSTR AboutBox::getDialogName()
  35. {
  36.     return "AboutBox";
  37. }
  38.  
  39. BOOL AboutBox::dispatch( HWND hDlg, WORD msg, WORD wParam, LONG lParam )
  40. {
  41.     switch( msg )
  42.         {
  43.  
  44.         case WM_INITDIALOG:
  45.  
  46.             return TRUE;        // no initialization.
  47.  
  48.         case WM_COMMAND:
  49.  
  50.             if( wParam == IDOK || wParam == IDCANCEL )
  51.                 {
  52.                 EndDialog( hDlg, TRUE );  // selecting OK or Cancel
  53.                 return TRUE;              // terminates the dialog
  54.                 }
  55.  
  56.         default:                // if we don't handle it, pass it
  57.                                 // to our parent.
  58.  
  59.             return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  60.  
  61.         }
  62. }
  63.  
  64. //---------------------------------------------------------------------
  65. //
  66. //  class SaveDir is only used locally, although it makes a handy
  67. //  addition to a toolbox.  When created it saves the current drive
  68. //  and directory, and when destroyed it restores the drive and path
  69. //  to the saved drive and directory.
  70. //
  71. //---------------------------------------------------------------------
  72.  
  73. class SaveDir
  74. {
  75. public:
  76.     SaveDir();
  77.     ~SaveDir();
  78. private:
  79.     int drive;
  80.     char path[MAXPATH];
  81. };
  82.  
  83. SaveDir::SaveDir()
  84. {
  85.     drive = getdisk();
  86.     path[0] = '\\';
  87.     getcurdir( 0, path+1 );
  88. }
  89.  
  90. SaveDir::~SaveDir()
  91. {
  92.     setdisk( drive );
  93.     chdir( path );
  94. }
  95.  
  96. //---------------------------------------------------------------------
  97. //
  98. //  member functions for class FileBox.
  99. //
  100. //---------------------------------------------------------------------
  101.  
  102. const fileAttr = 0x4010;
  103.  
  104. LPSTR FileBox::getDialogName()
  105. {
  106.     return "OpenFile";
  107. }
  108.  
  109. //---------------------------------------------------------------------
  110. //
  111. //  FileBox::FileBox( HWND owner,
  112. //                    const char *c,
  113. //                    const char *p,
  114. //                    const char *s,
  115. //                    BOOL me
  116. //                  );
  117. //
  118. //  constructor.  Massages initial data.
  119. //
  120. //---------------------------------------------------------------------
  121.  
  122. FileBox::FileBox( HWND owner,
  123.                   const char *c,
  124.                   const char *p,
  125.                   const char *s,
  126.                   BOOL me
  127.                 ) :
  128.     ModalDialog( owner ),
  129.     WinBase( ),
  130.     caption( c ),
  131.     iPath( p ),
  132.     iSpec( s ),
  133.     mustExist( me )
  134. {
  135.     strcpy( path, p );
  136.  
  137.     if( path[ strlen( path ) - 1 ] != '\\' )
  138.         strcat( path, "\\" );
  139.  
  140.     strcat( path, s );
  141. }
  142.  
  143. WORD FileBox::run()
  144. {
  145.     SaveDir sd;
  146.  
  147.     if( ModalDialog::run() != 0 )   // run() returns non-zero if
  148.         return 1;                   // the user selected Cancel
  149.     else
  150.         {
  151.         getcwd( res, sizeof res );  // otherwise, set up the selected
  152.         int l = strlen( res );      // path and file name in res.
  153.         if( res[ l-1 ] != '\\' )
  154.             strcat( res, "\\" );
  155.         strcat( res, path );
  156.         return 0;
  157.         }
  158. }
  159.  
  160. //---------------------------------------------------------------------
  161. //
  162. //  handy function for internal use
  163. //
  164. //---------------------------------------------------------------------
  165.  
  166.  
  167. void FileBox::resetDlg( HWND hDlg )
  168. {
  169.     DlgDirList( hDlg, path, IDD_FLIST, IDD_FPATH, fileAttr );
  170.                                     // set the directory list to the
  171.                                     // specified path.
  172.  
  173.     SetDlgItemText( hDlg, IDD_FNAME, path );
  174.                                     // set the file name to the
  175.                                     // specified name.
  176. }
  177.  
  178. //---------------------------------------------------------------------
  179. //
  180. //  void FileBox::initDlg( HWND hDlg );
  181. //
  182. //  initializes the FileBox dialog.
  183. //
  184. //---------------------------------------------------------------------
  185.  
  186.  
  187. void FileBox::initDlg( HWND hDlg )
  188. {
  189.     SendDlgItemMessage( hDlg, IDD_FNAME, EM_LIMITTEXT, MAXPATH, 0 );
  190.                                     // no more than MAXPATH characters
  191.                                     // in the file name.
  192.     resetDlg( hDlg );
  193.     SendMessage( hDlg, WM_SETTEXT, 0, (LONG)(LPSTR)caption );
  194.                                     // display the caption as the
  195.                                     // dialog title.
  196. }
  197.  
  198. //---------------------------------------------------------------------
  199. //
  200. //  BOOL FileBox::flistCmd( HWND hDlg, LONG lParam )
  201. //
  202. //  called when the user does something to the directory list box.
  203. //
  204. //---------------------------------------------------------------------
  205.  
  206. BOOL FileBox::flistCmd( HWND hDlg, LONG lParam )
  207. {
  208.  
  209.     switch( HIWORD( lParam ) )
  210.         {
  211.         case LBN_SELCHANGE:     // user changed the selection in the
  212.                                 // list box.
  213.  
  214.             if( DlgDirSelect( hDlg, path, IDD_FLIST ) != 0 )
  215.                 strcat( path, iSpec );
  216.                                 // current selection is a
  217.                                 // path, so append the file spec.
  218.  
  219.             SetDlgItemText( hDlg, IDD_FNAME, path );
  220.                                 // display the new path
  221.  
  222.             SendDlgItemMessage( hDlg,
  223.                                 IDD_FNAME,
  224.                                 EM_SETSEL,
  225.                                 0,
  226.                                 MAKELONG( 0, 0x7FFF )
  227.                                 );
  228.                                 // select all characters in the file
  229.                                 // name edit box.
  230.             return TRUE;
  231.  
  232.         case LBN_DBLCLK:        // user has double-clicked a selection
  233.                                 // in the list box.
  234.  
  235.             if( DlgDirSelect( hDlg, path, IDD_FLIST ) != 0 )
  236.                 strcat( path, iSpec );
  237.                                 // current selection is a
  238.                                 // path, so append the file spec.
  239.  
  240.             resetDlg( hDlg );
  241.             return TRUE;
  242.  
  243.         default:
  244.  
  245.             return FALSE;
  246.  
  247.         }
  248. }
  249.  
  250. //---------------------------------------------------------------------
  251. //
  252. //  BOOL FileBox::fnameCmd( HWND hDlg, LONG lParam );
  253. //
  254. //  called when the user does something in the edit box containing the
  255. //  file name.
  256. //
  257. //---------------------------------------------------------------------
  258.  
  259. BOOL FileBox::fnameCmd( HWND hDlg, LONG lParam )
  260. {
  261.     if( HIWORD( lParam ) == EN_CHANGE )
  262.         {                       // enables or disables the OK button
  263.                                 // depending on whether there are any
  264.                                 // characters in the edit box.
  265.         BOOL active =
  266.             (BOOL)SendMessage( LOWORD( lParam ), WM_GETTEXTLENGTH, 0, 0 );
  267.         EnableWindow( GetDlgItem( hDlg, IDOK ), active );
  268.         return TRUE;
  269.         }
  270.     else
  271.         return FALSE;
  272. }
  273.  
  274. //---------------------------------------------------------------------
  275. //
  276. //  void FileBox::okCmd( HWND hDlg );
  277. //
  278. //  called when the user selects the OK button.  Validates the file
  279. //  name.  Resets the dialog if the name is contains wildcards or is
  280. //  invalid, and terminates the dialog if it's a valid file name.
  281. //
  282. //---------------------------------------------------------------------
  283.  
  284. void FileBox::okCmd( HWND hDlg )
  285. {
  286.     GetDlgItemText( hDlg, IDD_FNAME, path, 80 );
  287.                                 // get the specified path.
  288.  
  289.     int len = strlen( path );
  290.     char lastChar = path[ len - 1 ];
  291.  
  292.     if( lastChar == '\\' || lastChar == ':' )
  293.         strcat( path, iSpec );  // if the path ends in '\' or ':' it's not
  294.                                 // a full spec, so append the default spec.
  295.  
  296.     if( strpbrk( path, "*?" ) != 0 )
  297.         {                       // if the path contains a wildcard,
  298.         resetDlg( hDlg );       // reset the dialog and continue.
  299.         return;
  300.         }
  301.  
  302.     if( mustExist == TRUE )     // if we require the file to exist, check it.
  303.         {
  304.         ffblk fileInfo;
  305.         if( findfirst( path, &fileInfo, 0 ) )
  306.             {
  307.             MessageBeep(0);     // file doesn't exist.
  308.             return;
  309.             }
  310.         }
  311.  
  312.     EndDialog( hDlg, TRUE );    // we're done!
  313. }
  314.  
  315. //---------------------------------------------------------------------
  316. //
  317. //  void FileBox::cancelCmd( HWND hDlg );
  318. //
  319. //  called when the user selects the Cancel button.  Terminates the
  320. //  dialog.
  321. //
  322. //---------------------------------------------------------------------
  323.  
  324. void FileBox::cancelCmd( HWND hDlg )
  325. {
  326.     result = 1;
  327.     EndDialog( hDlg, TRUE );
  328. }
  329.  
  330. //---------------------------------------------------------------------
  331. //
  332. //  BOOL FileBox::dispatch( HWND hDlg, WORD msg, WORD wParam, LONG lParam );
  333. //
  334. //  dispatches commands.
  335. //
  336. //---------------------------------------------------------------------
  337.  
  338. BOOL FileBox::dispatch( HWND hDlg, WORD msg, WORD wParam, LONG lParam )
  339. {
  340.     switch( msg )
  341.         {
  342.         case WM_INITDIALOG:
  343.  
  344.             initDlg( hDlg );
  345.             return TRUE;
  346.  
  347.         case WM_COMMAND:
  348.  
  349.             switch( wParam )
  350.                 {
  351.                 case IDD_FLIST:
  352.  
  353.                     if( flistCmd( hDlg, lParam ) == TRUE )
  354.                         return TRUE;
  355.                     break;
  356.  
  357.                 case IDD_FNAME:
  358.  
  359.                     if( fnameCmd( hDlg, lParam ) == TRUE )
  360.                         return TRUE;
  361.                     break;
  362.  
  363.                 case IDOK:
  364.  
  365.                     okCmd( hDlg );
  366.                     return TRUE;
  367.  
  368.                 case IDCANCEL:
  369.  
  370.                     cancelCmd( hDlg );
  371.                     return TRUE;
  372.  
  373.                 default:
  374.  
  375.                     break;
  376.                 }
  377.         default:
  378.  
  379.             break;
  380.         }
  381.  
  382.     return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  383.  
  384. }
  385.  
  386. //---------------------------------------------------------------------
  387. //
  388. //  member functions for class EditBox.
  389. //
  390. //---------------------------------------------------------------------
  391.  
  392. LPSTR EditBox::getDialogName()
  393. {
  394.     return "TodoEdit";
  395. }
  396.  
  397. //---------------------------------------------------------------------
  398. //
  399. //  void EditBox::initDlg( HWND hDlg );
  400. //
  401. //  initializes the dialog box.
  402. //
  403. //---------------------------------------------------------------------
  404.  
  405. void EditBox::initDlg( HWND hDlg )
  406. {
  407.     char temp[100];
  408.  
  409.     // set up the current date edit field.
  410.     ostrstream( temp, sizeof( temp ) ) << current.dateCreated << ends;
  411.     SetDlgItemText( hDlg, IDE_DATEENT, temp );
  412.  
  413.     // set up the date due edit field.
  414.     ostrstream( temp, sizeof( temp ) ) << current.dateDue << ends;
  415.     SetDlgItemText( hDlg, IDE_DATEDUE, temp );
  416.  
  417.     // set up the text edit field
  418.     SetDlgItemText( hDlg, IDE_TEXT, current.text );
  419.  
  420.     // set up the correct radio button
  421.     button = IDE_HIGH + 1 - current.priority;
  422.     CheckRadioButton( hDlg, IDE_LOW, IDE_HIGH, button );
  423. }
  424.  
  425. //---------------------------------------------------------------------
  426. //
  427. //  void EditBox::checkButton( HWND hDlg, WORD wParam );
  428. //
  429. //  called when the user selects one of the radio buttons.
  430. //
  431. //---------------------------------------------------------------------
  432.  
  433. void EditBox::checkButton( HWND hDlg, WORD wParam )
  434. {
  435.     button = wParam;
  436.     CheckRadioButton( hDlg, IDE_LOW, IDE_HIGH, button );
  437. }
  438.  
  439. //---------------------------------------------------------------------
  440. //
  441. //  void EditBox::okCmd( HWND hDlg );
  442. //
  443. //  called when the user selects the OK button.  Copies data from the
  444. //  dialog into the Todo entry and terminates the dialog.
  445. //
  446. //---------------------------------------------------------------------
  447.  
  448. void EditBox::okCmd( HWND hDlg )
  449. {
  450.     char temp[100 ];
  451.  
  452.     //  copy date created from dialog.
  453.     GetDlgItemText( hDlg, IDE_DATEENT, temp, sizeof( temp ) );
  454.     current.dateCreated = temp;
  455.  
  456.     //  copy date due from dialog.
  457.     GetDlgItemText( hDlg, IDE_DATEDUE, temp, sizeof( temp ) );
  458.     current.dateDue = temp;
  459.  
  460.     //  copy text from dialog
  461.     int len = (int)SendDlgItemMessage(hDlg,IDE_TEXT,EM_LINELENGTH,0,0) + 1;
  462.     delete current.text;
  463.     current.text = new char[ len ];
  464.     GetDlgItemText( hDlg, IDE_TEXT, current.text, len );
  465.  
  466.     //  copy priority from dialog.
  467.     current.priority = IDE_HIGH + 1 - button;
  468.  
  469.     //  mark this entry as modified.
  470.     current.dirty = TRUE;
  471.  
  472.     EndDialog( hDlg, TRUE );
  473. }
  474.  
  475. //---------------------------------------------------------------------
  476. //
  477. //  void EditBox::cancelCmd( HWND hDlg );
  478. //
  479. //  called when the user selects the Cancel button.  Terminates the
  480. //  dialog without changing any fields in the entry.
  481. //
  482. //---------------------------------------------------------------------
  483.  
  484. void EditBox::cancelCmd( HWND hDlg )
  485. {
  486.     result = 1;
  487.     EndDialog( hDlg, TRUE );
  488. }
  489.  
  490. //---------------------------------------------------------------------
  491. //
  492. //  BOOL EditBox::dispatch( HWND hDlg, WORD msg, WORD wParam, LONG lParam );
  493. //
  494. //  dispatches commands.
  495. //
  496. //---------------------------------------------------------------------
  497.  
  498. BOOL EditBox::dispatch( HWND hDlg, WORD msg, WORD wParam, LONG lParam )
  499. {
  500.     switch( msg )
  501.         {
  502.         case WM_INITDIALOG:
  503.  
  504.             initDlg( hDlg );
  505.             return TRUE;
  506.  
  507.         case WM_COMMAND:
  508.  
  509.             switch( wParam )
  510.                 {
  511.                 case IDE_LOW:
  512.                 case IDE_MEDIUM:
  513.                 case IDE_HIGH:
  514.  
  515.                     checkButton( hDlg, wParam );
  516.                     return TRUE;
  517.  
  518.                 case IDOK:
  519.  
  520.                     okCmd( hDlg );
  521.                     return TRUE;
  522.  
  523.                 case IDCANCEL:
  524.  
  525.                     cancelCmd( hDlg );
  526.                     return TRUE;
  527.                 }
  528.         }
  529.  
  530.     return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  531. }
  532.  
  533.  
  534.  
  535. 
  536.